package com.shouhuobao.bhi.msgnotice;

import android.app.Activity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.MsgNoticeBean;

import java.util.ArrayList;
import java.util.Date;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.lang.Str;

/**
 * 消息类型 <br>
 * 1路由 2 邮票过期 3投诉 4意见反馈 5节假日 6活动
 *
 * @author coffee<br>
 *         2015-10-23下午5:53:19
 */
public class MsgNoticeAdapter extends BaseAdapterExt<MsgNoticeBean> {

    public MsgNoticeAdapter(ArrayList<MsgNoticeBean> items, Activity context, ViewGroup listView) {
        super(items, context, listView);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.msg_center_item, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.msgcenter_title);
            holder.time = (TextView) convertView.findViewById(R.id.msgcenter_time);
            holder.expressCompany = (TextView) convertView.findViewById(R.id.msgcenter_express);
            holder.orderNum = (TextView) convertView.findViewById(R.id.msgcenter_order_num);
            holder.state = (TextView) convertView.findViewById(R.id.msgcenter_state);
            holder.desc = (TextView) convertView.findViewById(R.id.msgcenter_desc);
            holder.detail = (TextView) convertView.findViewById(R.id.msgcenter_detail);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        setInvisible(holder);
        MsgNoticeBean item = getItem(position);
        switch (item.getType()) {
            case 1: // 路由
                // xx的包裹已签收
                setText(holder.title, item.getTitle());
                setText(holder.time, item.getCreateTime());
                // setText(holder.expressCompany, "快递公司: " + item.getExpressCompanyName());
                // setText(holder.orderNum, "快递单号: " + item.getExpressNumber());
                // setText(holder.state, "状态: " + stateArr[item.getStatus()]);
                StringBuilder sb = new StringBuilder();
                sb.append("快递公司: " + item.getExpressCompanyName()).append("    ") //
                        .append("快递单号: " + item.getExpressNumber()).append("    ") //
                        .append("状态: " + item.getStatusStr());
                // holder.desc.setText(sb.toString());
                setText(holder.desc, sb.toString());
                holder.detail.setVisibility(View.VISIBLE);
                break;
            case 2:// 邮票通知
                setText(holder.title, item.getTitle());
                setText(holder.time, item.getCreateTime());
                setText(holder.desc, item.getShowContent());
                holder.detail.setVisibility(View.VISIBLE);
                break;
            case 6:// 活动
                setText(holder.title, item.getTitle());
                setText(holder.time, item.getCreateTime());
                holder.desc.setText(Html.fromHtml(item.getShowContent()));
                holder.desc.setVisibility(View.VISIBLE);
                holder.desc.setMovementMethod(LinkMovementMethod.getInstance());
                break;
            case 8:// 取件通知 -- 显示小箭头
            case 10:// 评价
                holder.detail.setVisibility(View.VISIBLE);
            case 11://包裹支付成功
            case 12://包裹投递成功
            case 13://包裹揽收成功
            case 14://包裹签收
            case 15://15购买邮票成功
            case 16://邮票过期提醒
            case 17://身份认证消息
            case 18://审核失败驳回
            case 19://火箭侠揽收包裹
            case 20://火箭侠拒收包裹
            case 21://超时赔付通知
                setText(holder.title, item.getTitle());
                setText(holder.time, item.getCreateTime());
                setText(holder.desc, item.getContent().replace("{", "[").replace("}", "]"));
                holder.detail.setVisibility(View.VISIBLE);
                break;
            default: // 3投诉 4 意见反馈 5 节假日 8 取件通知 10、评价
                setText(holder.title, item.getTitle());
                setText(holder.time, item.getCreateTime());
                setText(holder.desc, item.getShowContent());
                break;
        }
        return convertView;
    }

    private void setInvisible(ViewHolder holder) {
        holder.title.setVisibility(View.GONE);
        holder.time.setVisibility(View.GONE);
        holder.expressCompany.setVisibility(View.GONE);
        holder.orderNum.setVisibility(View.GONE);
        holder.state.setVisibility(View.GONE);
        holder.desc.setVisibility(View.GONE);
        holder.detail.setVisibility(View.GONE);
    }

    private void setText(TextView view, String text) {
        if (view.getId() == R.id.msgcenter_time) {
            if (Str.isEmpty(text)) {
                return;
            }
            long millis = Long.valueOf(text);
            String timeStr = droid.frame.utils.sqlite.DateUtils.format(new Date(millis));
            // 超过一天则显示年月日
            if (System.currentTimeMillis() - millis > 24 * 60 * 60 * 1000) {
                text = timeStr.substring(0, timeStr.length() - 3);
                String timeStrNow = droid.frame.utils.sqlite.DateUtils.format(new Date(System.currentTimeMillis()));
                if (timeStr != null && timeStrNow != null) {
                    if (timeStrNow.length() > 4 && timeStr.length() > 4) {
                        String timeStrYear = timeStr.substring(0, 4);
                        String timeStrNowYear = timeStrNow.substring(0, 4);
                        // 如果是同一年，不显示年份
                        if (timeStrYear.equals(timeStrNowYear)) {
                            // 截去年份
                            text = timeStr.substring(5, timeStr.length() - 3);
                        }
                    }
                }

            } else {
                int start = timeStr.indexOf(" ");
                text = timeStr.substring(start + 1, start + 6);
            }
        }
        view.setText(text);
        view.setVisibility(View.VISIBLE);
    }

    class ViewHolder {
        TextView title;
        TextView time;
        TextView expressCompany;
        TextView orderNum;
        TextView state;
        TextView desc;
        View detail; // 右箭头、代表可以查看详情
    }
}
